package com.farmer.controller;


import com.farmer.pojo.Agritainment;
import com.farmer.pojo.Goods;
import com.farmer.service.GoodsService;
import com.farmer.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("add")
    public Result add(@RequestBody Goods goods) {
        Result result =goodsService.add(goods);
        return result;
    }

    @DeleteMapping("delete")
    public Result delete(@RequestParam Integer id) {
        Result result = goodsService.delete(id);
        return result;
    }
    @DeleteMapping("delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        Result result = goodsService.deleteBatch(ids);
        return result;
    }
    @PutMapping("update")
    public Result update(@RequestBody Goods goods) {
        Result result = goodsService.update(goods);
        return result;
    }

    @GetMapping("getById")
    public Result getById(@RequestParam Integer id) {
        Result result = goodsService.getById(id);
        return result;
    }

    @GetMapping("getAll")
    public Result getAll() {
        Result result = goodsService.getAll();
        return result;
    }

    @GetMapping("getByTitleLike")
    public Result getByTitleLike(@RequestParam String title){
        Result result = goodsService.getByTitleLike(title);
        return result;
    }

    @PostMapping("imgInsert")
    public Result imgInsert(@RequestParam Integer id,@RequestBody MultipartFile file,@RequestParam(required = false) String name,@RequestParam(required = false) Double price, @RequestParam(required = false)  Integer num,@RequestParam(required = false) Integer sales){
        Result result = goodsService.ImgInsert(id,file,name,price,num,sales);
        return result;
    }
    @GetMapping("page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        Result result=goodsService.page(pageNum,pageSize);
        return result;
    }
}


